import 'package:sample2/helper/app_helper.dart';
import 'package:sample2/model/chat/chat_channel.dart';
import 'package:sample2/model/chat/chat_constants.dart';
import 'package:sample2/model/chat/chat_group.dart';
import 'package:sample2/model/chat/chat_user.dart';
import 'package:sample2/model/chat/message.dart';
import 'package:sample2/model/user_info.dart';
import 'package:sample2/utils/networking.dart';

class ChatService {

  static Future<String?> createChannel({
          required String adminId,
          required String channelName,
          String? channelDesc,
          String? channelTag,
        }) {
        return NetworkUtil.post("chat/channel/create", {}, {
          "adminId": adminId,
          "channelName": channelName,
          "channelDesc": channelDesc,
          "channelTag": channelTag,
        }).then((NetworkResponseObject responseObject) {
          return responseObject.content?["channelId"];
        });
  }


  static Future<List<ChatChannel>>? getChannels() {
    return NetworkUtil.post("chat/channel/list", {}, {})
        .then((NetworkResponseObject responseObject) {
      List<ChatChannel> channels = [];

      if (responseObject.respondCode == 0) {
        List channelsArr = responseObject.content?["channels"];
        channels = channelsArr.map((e) {
          return ChatChannel(
            channelId: e['channelid'],
            channelTag: e['channeltag'],
            channelDesc: e['channeldesc'],
            createTime: e['createtime'],
            channelName: e['channelname'],
          );
        }).toList();
      }
      return channels;
    });
  }

  static Future<String?> createGroup({
    required String adminId,
    required String channelId,
    required String groupName,
    String? groupDesc,
  }) {
    return NetworkUtil.post("chat/group/create", {}, {
      "adminId": adminId,
      "channelId": channelId,
      "groupName": groupName,
      "groupDesc": groupDesc,
    }).then((NetworkResponseObject responseObject) {
      return responseObject.content?["groupId"];
    });
  }

  static Future<List<ChatGroup>>? getGroups(String channelId) {
    return NetworkUtil.post(
            "chat/group/list", {}, {"channelId": channelId})
        .then((NetworkResponseObject responseObject) {
      List<ChatGroup> groups = [];
      if (responseObject.respondCode == 0) {
        List groupArr = responseObject.content?["groups"];
        groups = groupArr.map((e) {
          return ChatGroup(
              chatId: e['groupId'],
              groupId: e['groupId'],
              ownerId: e['ownerId'],
              channelId: e['channelId'],
              groupName: e['groupName'],
              groupDesc: e['groupDesc'],
              groupStatus: e['gpStatus']);
        }).toList();
      }
      return groups;
    });
  }

  static Future<List<ChatMessageUser>>? getChatUsers() {
    return NetworkUtil.get("chat/getUsers", {})
        .then((NetworkResponseObject responseObject) {
      List<ChatMessageUser> channels = [];

      if (responseObject.respondCode == 0) {
        List users = responseObject.content?["users"];
        channels = users.map((aUser) {
          return ChatMessageUser(
              userName: aUser["username"].toString(),
              userId: aUser["userid"].toString(),
              time: aUser["time"].toString(),
              chatId: aUser["chatid"].toString(),
              status: ChatPeerStatus.values[aUser["status"]]);
        }).toList();
      }
      return channels;
    });
  }

  static Future<List<ChatMessage>?> getGroupMessages(
      {required String groupId, int? pageSize}) {
    return NetworkUtil.post(
            "chat/group/msg/history", {}, {"groupId": groupId})
        .then((NetworkResponseObject responseObject) {
      List msgs = responseObject.content?["messages"];
      return AppHelper.shared.getUserInfo().then((UserInfo? userInfo) {
        List<ChatMessage> messages = msgs.map((msg) {
          ChatMessage message = ChatMessage.fromJsonMap(msg);
          message.messageSenderReceiverType = userInfo?.userid == message.from
              ? ChatMessageSenderReceiverType.sender
              : ChatMessageSenderReceiverType.receiver;
          return message;
        }).toList();
        return messages;
      });
    });
  }

  static Future<List<ChatMessage>?> getMessage(
      {required String from, required String to}) {
    return NetworkUtil.post(
            "chat/msg/history", {}, {"from": from, "to": to})
        .then((NetworkResponseObject responseObject) {
      List msgs = responseObject.content?["messages"];
      return AppHelper.shared.getUserInfo().then((UserInfo? userInfo) {
        List<ChatMessage> messages = msgs.map((msg) {
          ChatMessage message = ChatMessage.fromJsonMap(msg);
          message.messageSenderReceiverType = userInfo?.userid == message.from
              ? ChatMessageSenderReceiverType.sender
              : ChatMessageSenderReceiverType.receiver;
          return message;
        }).toList();
        return messages;
      });
    });
  }
}
